from mvvmQt.Parser import Parser
from mvvmQt.Observable import Observable, Computed, DebounceAsync
from mvvmQt.rewriteContorls import Window
from PyQt5.QtCore import pyqtSignal, QThread
from PyQt5.QtWidgets import QLabel
from PyQt5.QtCore import Qt, pyqtSignal
import asyncio
from qasync import asyncSlot

class ClickLabel(QLabel):
    clicked = pyqtSignal()
    def mouseReleaseEvent(self, QMouseEvent):
        if QMouseEvent.button() == Qt.LeftButton:
            self.clicked.emit()

class Model:
    def __init__(self):
        self.pos = Observable('200, 200')
        self.spacing = Observable(10)
        self.spacing1 = Computed([self.spacing], lambda v: v[0] > 0)
        self.repeat = Observable(True)
        self.tipShow = Observable(False)
        self.selectIndex = Observable(2)
        self.isDown = Observable(False)
        self.isDown.subscribe(self.down_print)
        self.html = Observable('<b>改点内容试试</b>')
        self.html.subscribe(lambda v: self.test('html', v))
        self.dialValue = Observable(10)

        self.debounce_spacing = Observable(self.spacing.value)
        self.spacing.subscribe(self.setValue)
        self.debounce_spacing.subscribe(print)

        self.svg = Observable('<svg></svg>')

    def test(self, name, v):
        print("%s changed %s" % (name, v))

    async def down_print(self, v):
        print("isDown changed %d" % v)

    @DebounceAsync(1)
    def setValue(self, v):
        self.debounce_spacing.value = v

class Event:
    def __init__(self, models: Model):
        self.models = models

    def addSpacing(self, ele):
        self.models.spacing.value += 1

    def downSpacing(self, ele):
        if self.models.spacing.value > 0:
            self.models.spacing.value -= 1

    def showTips(self, ele):
        self.models.tipShow.value = not self.models.tipShow.value
        if self.models.tipShow.value:
            self.models.pos.value = '300, 300'
        else:
            self.models.pos.value = '200, 200'

    async def change(self, ele, v):
        # 第一个参数必然是对应的Element对象，第二个参数为事件传递的参数
        await asyncio.sleep(1)
        print('change', v, type(v))

    def testSignal(self, ele):
        w = p.idElements['mainWindow'].qt
        w.aSignal.emit(123)

    def toggleIsDown(self, ele):
        self.models.isDown.value = not self.models.isDown.value

    async def test_label_click(self, ele):
        await asyncio.sleep(1)
        print(ele)

class CParser(Parser):
    def build(self):
        super().build()
        w = p.idElements['mainWindow'].qt
        th = Thread(10, w)
        th.bSignal.connect(self.test)

        th2 = Thread(100, w)
        th2.bSignal.connect(self.test)
        th.start(QThread.Priority.HighestPriority)
        th2.start(QThread.Priority.LowestPriority)

    def test(self, i):
        print('bSignal emit', i)

class MainWindow(Window):
    aSignal = pyqtSignal(int)

class Thread(QThread):
    bSignal = pyqtSignal(int)
    def __init__(self, num, parent=None):
        super().__init__(parent)
        self.parent = parent
        self.num = num
        self.isWorking = True

    def run(self):
        for i in range(100):
            # print(self.num ** 2)
            pass

    def test(self, s):
        print('aSignal emit', s, self.priority())

models = Model()
events = Event(models)
p = CParser('./', models=models, events=events)
p.addQtClass('mainWindow', MainWindow)
p.addQtClass('ClickLabel', ClickLabel)
p.build()
p.run()